package login;

import backstage.BackstageClassifyBean;
import dbTools.DBTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 * Servlet implementation class Updateserver
 */
@WebServlet("/Updateserver")
public class Updateserver extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Updateserver() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//��ȡ�����û���
		response.setContentType("text/html;charset=GBK");
		request.setCharacterEncoding("GBK");
		response.setCharacterEncoding("GBK");
		HttpSession session = request.getSession();
		String userName = (String) session.getAttribute("userName");
		//��ȡ������
		String userPwd = request.getParameter("userPwd");
		//��ȡ��������
		String pwd = request.getParameter("pwd");
		//******���ݿⲿ��*******
		
		DBTools db = new DBTools();
		db.open();
		String sql = "SELECT * FROM userInfo WHERE userName = '"+userName+"'";
		ArrayList al=new ArrayList();
		ResultSet rs = db.select(sql);
		try {
			while(rs.next()){
				BackstageClassifyBean bcb=new BackstageClassifyBean(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4));
				al.add(bcb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		db.close();
		BackstageClassifyBean bcb=(BackstageClassifyBean)al.get(0);
		if(!bcb.getUserPwd().equals(userPwd)){
			session.setAttribute("error", "true");
			request.getRequestDispatcher("/Modifies.jsp").forward(request, response);
		}else{
			PrintWriter out = response.getWriter();
			db.open();
			//��ʼ�޸�
			String updateSql = "UPDATE userInfo SET userPwd = '"+pwd+"' WHERE userName = '"+userName+"'";
			db.update(updateSql);
			db.close();
			session.removeAttribute("userName");
			out.print("<script language='javascript'>window.parent.frames.location.href='login.jsp'</script>");
		}
	}

}
